/**
 * 
 */

function validateForm() {
	var div = document.getElementById("error");
	while (div.hasChildNodes()) {
		div.removeChild(div.childNodes[0]);
	}
	var restaurantName = document.forms["addrestaurant"]["restaurantName"].value;
	var openTime = document.forms["addrestaurant"]["openTime"].value;
	var closeTime = document.forms["addrestaurant"]["closeTime"].value;
	var deliveryPrice = document.forms["addrestaurant"]["deliveryPrice"].value;
	var minimumBill = document.forms["addrestaurant"]["minOrder"].value;
	var paymentmethod = document.forms["addrestaurant"]["paymentmethod"].value;

	var check = true;
	var textError = null;
	if (restaurantName == null || restaurantName == "") {
		textError = "Restaurant name must be filled out";
		addElement(div, textError);
		check = false;
	}

	if (closeTime == null || closeTime == "") {
		textError = "Close Time must be filled out";
		addElement(div, textError);
		check = false;
	}

	if (openTime == null || openTime == "") {
		textError = "Open Time must be filled out";
		addElement(div, textError);
		check = false;
	}

	if (deliveryPrice == null || deliveryPrice == "") {
		textError = "Delivery Price must be filled out";
		addElement(div, textError);
		check = false;
	}

	if (minimumBill == null || minimumBill == "") {
		textError = "Minimum Bill must be filled out";
		addElement(div, textError);
		check = false;
	}

	if (paymentmethod == 0) {
		textError = "Payment Method must be filled out";
		addElement(div, textError);
		check = false;
	}
	if (checkExistedRestaurant() == true) {
		textError = "Restaurant Name has existed!";
		addElement(div, textError);
		check = false;
	}

	return check;
}

function addElement(div, textError) {

	var dv = document.createElement("DIV");
	dv.setAttribute("class", "alert alert-danger");
	var sp1 = document.createElement("SPAN");
	sp1.setAttribute("class", "glyphicon glyphicon-exclamation-sign");
	dv.appendChild(sp1);

	var text = document.createTextNode(textError);

	dv.appendChild(text);
	div.appendChild(dv);
}

function checkExistedRestaurant() {
	var flag = false;
	$.ajax({
		"type" : "GET",
		"async" : false,
		"url" : "/core-web-admin/restaurant/checkexistedrestaurant",
		"data" : {
			"restaurantName" : $('#restaurantName').val()
		},
		"success" : function(data) {
			if (data == "true") {
				flag = true;
			}
			if (data == "false") {
				flag = false;
			}
		},
		"error" : function(data) {
			alert("ERROR WHEN CHECK EXISTED RESTAURANT NAME!");
			flag = false;
		}
	});
	return flag;
}

function validateForm() {
	var div = document.getElementById("error");
	while (div.hasChildNodes()) {
		div.removeChild(div.childNodes[0]);
	}
	var openTime = document.forms["addrestaurant"]["openTime"].value;
	var closeTime = document.forms["addrestaurant"]["closeTime"].value;
	var deliveryPrice = document.forms["addrestaurant"]["deliveryPrice"].value;
	var minimumBill = document.forms["addrestaurant"]["minOrder"].value;
	var paymentmethod = document.forms["addrestaurant"]["paymentmethod"].value;

	var check = true;
	var textError = null;

	if (closeTime == null || closeTime == "") {
		textError = "Close Time must be filled out";
		addElement(div, textError);
		check = false;
	}

	if (openTime == null || openTime == "") {
		textError = "Open Time must be filled out";
		addElement(div, textError);
		check = false;
	}

	if (deliveryPrice == null || deliveryPrice == "") {
		textError = "Delivery Price must be filled out";
		addElement(div, textError);
		check = false;
	}

	if (minimumBill == null || minimumBill == "") {
		textError = "Minimum Bill must be filled out";
		addElement(div, textError);
		check = false;
	}

	if (paymentmethod == 0) {
		textError = "Payment Method must be filled out";
		addElement(div, textError);
		check = false;
	}

	return check;
}
